var params = {}, tableIns;
layui.use(['table','layer','jquery','form'], function(){
    var $ = jQuery = layui.$;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    tableIns = table.render({
        elem: '#columnstable',
        height:'full-113',
        size:'sm',
        url: ctx+'/sys_admin/modules/getColumnsList?moduleid='+moduleid,
        page: true,
        even: true,
        cols: [[
            {type: 'numbers',fixed: 'left'},
            {field: 'id', title: 'ID',fixed: 'left', align:'center',width:100, hide:true},
            {field: 'column_comment', title: '字段名称',fixed: 'left', align:'center',width:200},
            {field: 'column_name', title: '库表名称', align:'center',width:180, hide:true},
            {field: 'columntype', title: '字段类型', align:'center',width:180},
            {field: 'lengths', title: '长度', align:'center',width:180},
            {field: 'decimals', title: '小数位', align:'center',width:180},
            {field: 'column_default', title: '默认值', align:'center'},
            {field: 'is_nullable', title:'是否可为空', templet: function(d){
                    if (d.is_nullable == 'YES') {
                        return '是';
                    } else {
                        return '否';
                    }
                }, unresize: true, width:150},
            {field: 'is_show', title:'是否显示', templet: '#showable', unresize: true, width:150},
            {title: '操作', width: 280, fixed: 'right', align:'center',toolbar: '#toolbar'}
        ]],
        done:function(res, curr){
            params['curr'] = curr;
        }
    });
    table.on('tool(columns)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('确定删除行么？', function(index){
                $.post(ctx+'/sys_admin/modules/deleteColumns', {"ids":data.id,"tablename":tablename}, function(result) {
                    if (result.success) {
                        obj.del();
                        layer.msg("删除成功!")
                        layer.close(index);
                    } else {
                        layer.msg(result.msg);
                    }
                }, 'json');
            });
        } else if(obj.event === 'insert'){
            parent.layer.open({
                title:"插入",
                type: 2,
                area: ['60%', '60%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/columnsForm?tablename='+tablename+'&moduleid='+moduleid+'&target='+data.column_name,
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = location;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn3: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index);
                }
            });
        } else if(obj.event === 'moveup'){
            layer.confirm('确定要上移吗？', function(index){
                $.post(ctx+'/sys_admin/modules/move', {"tablename": tablename, "column":data.column_name, "updown":1, "columnid":data.id}, function(result) {
                    if (result.success) {
                        layer.msg("上移成功!")
                        location.reload();
                    } else {
                        layer.msg(result.msg);
                    }
                }, 'json');
            });
        } else if(obj.event === 'movedown'){
            layer.confirm('确定要下移吗？', function(index){
                $.post(ctx+'/sys_admin/modules/move', {"tablename": tablename, "column":data.column_name, "updown":2, "columnid":data.id}, function(result) {
                    if (result.success) {
                        layer.msg("下移成功!")
                        location.reload();
                    } else {
                        layer.msg(result.msg);
                    }
                }, 'json');
            });
        } else if(obj.event === 'edit'){
            parent.layer.open({
                title:"编辑",
                type: 2,
                area: ['60%', '60%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/columnsForm?tablename='+tablename+'&moduleid='+moduleid+'&column_name='+data.column_name+'&id='+data.id,
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = location;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn3: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index);
                }
            });
        }
    });

    form.on('select(status)', function(data){
        $('#submitid').click();
    });

    form.on('switch(isshow)', function(data){
        var selectIfKey = data.othis;
        var parentTr = selectIfKey.parents("tr");
        var parentTrIndex = parentTr.attr("data-index");
        var id = parentTr.find(('td:eq(1)')).text().trim();
        var state = 0;
        if(data.elem.checked){state = 1;}
        $.post(ctx+'/sys_admin/modules/setColumnShow', {ids: id, isshow: state}, function() {}, 'json');
    });

    $("#add").on('click',function(){
        parent.layer.open({
            type: 2,
            area: ['60%', '60%'],
            fixed: false, //不固定
            maxmin: true,
            content: ctx+'/sys_admin/modules/columnsForm?tablename='+tablename+'&moduleid='+moduleid+'&target=createuser',
            btn: ['提交', '重置','取消'],
            yes: function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = location;
                iframeWin.contentWindow.params = params;
                layero.find('iframe').contents().find('#submit-btn').click();
            },
            btn2: function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = location;
                layero.find('iframe').contents().find('#reset-btn').click();
                return false;
            },
            btn3: function(index, layero){
                var index = layer.getFrameIndex(window.name);
                layer.close(index);
            }
        });
    });

});